<script>
	import {
		mapActions,
		mapMutations
	} from "vuex";
	import {
		router
	} from '@/router';
	import {
		getConfig
	} from "@/api/app";
	import {
		strToParams
	} from "@/utils/tools";
	import {
		bindSuperior
	} from "@/api/user";
	import {
		SHOP_CODE,
		QUERY_CODE
	} from "@/config/cachekey";
	import Cache from "@/utils/cache";
	export default {
		onLaunch: function(options) {
			// 获取配置
			this.getSystemInfo();
			this.getUser();
			this.getShop();
			//#ifdef MP-WEIXIN
			this.checkMpUpdate();
			//#endif

			this.bindCode(options);
			this.$isResolve();
		},
		onShow(options) {
			let query_code = options.query;
			Cache.set(QUERY_CODE, query_code);
		},
		methods: {
			...mapActions(["getSystemInfo", "getUser", "getShop", "wxShare", "initLocationFunc"]),
			...mapMutations(["setConfig"]),
			//#ifdef MP-WEIXIN
			checkMpUpdate() {
				const updateManager = wx.getUpdateManager();
				updateManager.onUpdateReady(function() {
					wx.showModal({
						title: "更新提示",
						content: "新版本已准备好，是否重启？",
						success: function(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						},
					});
				});
			},
			//#endif
			async bindCode(options) {
				if (!options.query) return;
				let shop_code =
					options.query.shop_code ||
					strToParams(decodeURIComponent(options.query.scene)).shop_code;
				if (shop_code) {
					Cache.set(SHOP_CODE, shop_code);
				} else {
					if (!Cache.get(SHOP_CODE)) {
						await this.getConfigFun();
					}
				}

			},
			async getConfigFun() {
				const {
					code,
					data
				} = await getConfig();
				if (code == 1) {
					this.setConfig(data);
					if (data.is_open_nearby) {
						const {
							currentRoute
						} = router
						if (!currentRoute.meta.noBindCode) {
							// 获取定位
							this.initLocationFunc();
						}
					}
					// 防止第一次调用时拿不到数据
					this.wxShare();
				}
			}
		},
	};
</script>
<style lang="scss">
	@import "styles/common.scss";
	@import "components/uview-ui/index.scss";
	@import url("@/plugin/emoji-awesome/css/apple.css");
	/*每个页面公共css */
</style>